<template>
  <div>
    <BasicModal
      v-bind="$attrs"
      @register="register"
      :title="modalTitle"
      width="1200px"
      wrapClassName="j-user-select-modal"
      @ok="handleOk"
      destroyOnClose
      @visible-change="visibleChange"
    >
      <a-row>
        <a-col :span="showSelected ? 18 : 24">
          <BasicTable
            :columns="columns"
            :bordered="true"
            :useSearchForm="true"
            :formConfig="formConfig"
            :api="getGroupPersonList"
            :searchInfo="searchInfo"
            :rowSelection="rowSelection"
            :indexColumnProps="indexColumnProps"
            v-bind="getBindValue"
          ></BasicTable>
        </a-col>
        <a-col :span="showSelected ? 6 : 0">
          <BasicTable
            v-bind="selectedTable"
            :dataSource="selectRows"
            :useSearchForm="true"
            :formConfig="{ showActionButtonGroup: false, baseRowStyle: { minHeight: '40px' } }"
          >
            <!--操作栏-->
            <template #action="{ record }">
              <a href="javascript:void(0)" @click="handleDeleteSelected(record)"><Icon icon="ant-design:delete-outlined"></Icon></a>
            </template>
          </BasicTable>
        </a-col>
      </a-row>
    </BasicModal>
  </div>
</template>
<script lang="ts">
  import { defHttp } from '/@/utils/http/axios';
  import { defineComponent, ref, unref,watch } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { getGroupPersonList } from '/@/api/common/api';
  import { createAsyncComponent } from '/@/utils/factory/createAsyncComponent';
  import { useSelectBiz } from '/@/components/Form/src/jeecg/hooks/useSelectBiz';
  import { useAttrs } from '/@/hooks/core/useAttrs';
  import { selectProps } from '/@/components/Form/src/jeecg/props/props';


  export default defineComponent({
    name: 'PersonSelectModal',
    components: {
      //此处需要异步加载BasicTable
      BasicModal,
      BasicTable: createAsyncComponent(() => import('/@/components/Table/src/BasicTable.vue'), {
        loading: true,
      }),
    },
    props: {
      ...selectProps,
      //选择框标题
      modalTitle: {
        type: String,
        default: '人员选择',
      },
      groupId: {
        type: String,
        default: () => '',
      },
      //  otherParams: {
      //   type: Object,
      //   default: () => {},
      // },
    },
    emits: ['register', 'getSelectResult'],
    setup(props, { emit, refs }) {
      //注册弹框
      const [register, { closeModal }] = useModalInner( async(data)=>{
         searchInfo.value = {
              groupIds: data.groupId
         }
      })
      
      const attrs = useAttrs();
      //表格配置
      const config = {
        canResize: false,
        bordered: true,
        size: 'small',
        //改成读取rowKey,自定义传递参数
        rowKey: props.rowKey,
      };
      const getBindValue = Object.assign({}, unref(props), unref(attrs), config);
      const [{ rowSelection, visibleChange, indexColumnProps, getSelectResult, handleDeleteSelected, selectRows }] = useSelectBiz(
        getGroupPersonList,
        getBindValue
      );
      // const searchInfo = ref(props.params);
      const searchInfo = ref({
        groupIds: props.groupId
      });

      //查询form
      const formConfig = {
        labelCol: {
          span: 4,
        },
        baseColProps: {
          xs: 24,
          sm: 10,
          md: 10,
          lg: 10,
          xl: 10,
          xxl: 10,
        },
        //update-begin-author:liusq date:2023-10-30 for: [issues/5514]组件页面显示错位
        actionColOptions: {
            xs: 24,
            sm: 8,
            md: 8,
            lg: 8,
            xl: 8,
            xxl: 8,
        },
        //update-end-author:liusq date:2023-10-30 for: [issues/5514]组件页面显示错位
        schemas: [
          {
            label: '姓名',
            field: 'name',
            component: 'Input',
            colProps: { span: 4 },
          },
          {
            label: '学号',
            field: 'code',
            component: 'Input',
            colProps: { span: 4},
          },
          {
            label: "部门",
            field: 'departmentId',
            component : 'JSelectDept',
            componentProps:{
             multiple:false,
             checkable:false,
            },
            colProps: { span: 6},
          },
          // {
          //     label: "分组",
          //     field: 'groupId',
          //     component: 'JSearchSelect',
          //     componentProps: {
          //       dict: 'ac_group,group_name,id',
          //     },
          //     show:false,
          //     //colProps: {span: 6},
          // },
        ],
      };
      //定义表格列
      const columns = [
        {
          title: '学号',
          dataIndex: 'code',
          align: 'left',
        },
        {
          title: '姓名',
          dataIndex: 'name',
          // width: 180,
        },
        {
          title: '部门',
          dataIndex: 'departmentId_dictText',
        },
         {
          title:"通行分组",
          align:"center",
          dataIndex: 'groupName_dictText',
        },
      ];
      //已选择的table信息
      const selectedTable = {
        pagination: false,
        showIndexColumn: false,
        scroll: { y: 390 },
        size: 'small',
        canResize: false,
        bordered: true,
        rowKey: 'id',
        columns: [
          {
            title: '姓名',
            dataIndex: 'name',
            width: 40,
          },
          {
            title: '操作',
            dataIndex: 'action',
            align: 'center',
            width: 40,
            slots: { customRender: 'action' },
          },
        ],
      };
      /**
       * 确定选择
       */
      function handleOk() {
        getSelectResult((options, values) => {
          //回传选项和已选择的值
          emit('getSelectResult', options, values);
          console.log(values);
           console.log(options);
          //关闭弹窗
          closeModal();
        });
      }
      return {
        handleOk,
        getGroupPersonList,
        register,
        visibleChange,
        getBindValue,
        formConfig,
        indexColumnProps,
        columns,
        rowSelection,

        selectedTable,
        selectRows,
        handleDeleteSelected,
        searchInfo,
      };
    },
  });
</script>
